getProviderList = function() {
	$("#selectSupplierNo").val("");
	$("#providerList").dataTable().fnDestroy();
	$("#providerList").dataTable({
		"processing" : true,
		"serverSide" : false,
		"paginationType" : "full_numbers",
		"ajax" : "/Medical_Instrument/firstEnterprise/getProviderList",
		"aaSorting" : [ [ 0, "desc" ] ],
		"aoColumnDefs" : [ {
			"orderable" : false,
			"aTargets" : [ 1, 2, 3, 4, 5, 6]
		} ],
		"columns" : [ {
			"data" : "supplierEnterpriseNo",
			"defaultContent" : ""
		}, {
			"data" : "enterpriseName",
			"defaultContent" : ""
		}, {
			"data" : "enterpriseName",
			"defaultContent" : ""
		}, {
			"data" : "registerAddress",
			"defaultContent" : ""
		}, {
			"data" : "linkman",
			"defaultContent" : ""
		}, {
			"data" : "linkphone",
			"defaultContent" : ""
		}, {
			"data" : "remark",
			"defaultContent" : ""
		} ]
	});
}

// 验证身份证号
checkIdentityCard = function() {
	$("#repetition").val("");
	var supplierNo = $("#selectSupplierNo").val();
	if (supplierNo == "")
		return;
	var identityCard = $("#identityCard").val();
	$.ajax({
		type : "POST",
		url : "/Medical_Instrument/supplierPeople/checkIdentityCard",
		data : {
			"supplierNo" : supplierNo,
			"identityCard" : identityCard
		},
		dataType : "json",
		success : function(data) {
			if (data.result == "true") {
				layer.msg('该供应商已有该供方人员!', {
					icon : 5,
					time : 2000
				});
				$("#repetition").val("true");
			}

		}
	});
}

// 新增供方人员资质保存
saveSupplierPeopleInfo = function() {
	if ($("#selectSupplierNo").val() == "") {
		layer.msg('请选择供应商!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	if ($("#name").val() == "") {
		layer.msg('请填写供方人员姓名!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	if ($("#identityCard").val() == "") {
		layer.msg('请填写供方人员身份证号!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	
	if ($("#identityCard").val().length >18) {
		layer.msg('请正确输入18位的身份证号!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	if ($("#repetition").val() == "true") {
		layer.msg('该供应商已有该供方人员!', {
			icon : 5,
			time : 2000
		});

		return;
	}

	if ($("#area").val() == "") {
		layer.msg('请选择供方人员授权区域!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	if ($("#accreditEndDate").val() == "") {
		layer.msg('请填写授权结束日期!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	if ($("#recordsEndDate").val() == "") {
		layer.msg('请填写备案结束日期!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	if ($("#accreditPeople").val() == "") {
		layer.msg('请填写授权人!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	if (trim($("#business_scope").val()) == "") {
		layer.msg('请设置商品经营范围!', {
			icon : 5,
			time : 1000
		});
		return;
	}

	if (getDays($("#accreditEndDate").val(), $("#accreditStartDate").val()) > 365) {
		layer.msg('授权结束日期不能超过一年!', {
			icon : 5,
			time : 1000
		});
		return;
	}

	if (getDays($("#recordsEndDate").val(), $("#accreditStartDate").val()) > 365) {
		layer.msg('备案结束日期不能超过一年!', {
			icon : 5,
			time : 1000
		});
		return;
	}

	$("#confirm").attr("data-dismiss", "modal");

	if ($('#isVarietyControl').is(':checked')) {
		$('#varietyControl').val(1);
	}

	var classifyNoStr = "";
	var i = 0;
	$("#selectedCategory").find("tr").each(function() {
		var tdArr = $(this).children();
		var classifyNo = tdArr.eq(0).next().text();
		if (i != 0) {
			classifyNoStr += classifyNo + ";";
		}
		i++;
	});

	// 得到{键:值}这种类型的数据
	var data = $("#supplierPeopleInfo").serializeJson();
	// 然后创建一个对象把表单信息和数组封起来
	var object = {};
	object.supplierPeople = data;
	object.classifyNoStr = classifyNoStr;
	if ($("#isAddOrUpdate").val() == "add") {
		$.ajax({
			type : "POST",
			url : "/Medical_Instrument/supplierPeople/addSupplierPeople",
			data : JSON.stringify(object),
			contentType : "application/json",
			dataType : "json",
			success : function(data) {
				if (data.result == "success") {
					layer.msg('新增成功!', {
						icon : 6,
						time : 1000
					});
					chang_content(null, 1)
				}

				if (data.result == "failed") {
					layer.msg('新增失败!', {
						icon : 5,
						time : 1000
					});
				}

			},
			error : function(data) {
				layer.msg('请求失败!', {
					icon : 5,
					time : 1000
				});
			}
		});
	} else {
		$.ajax({
			type : "POST",
			url : "/Medical_Instrument/supplierPeople/updateSupplierPeople",
			data : JSON.stringify(object),
			contentType : "application/json",
			dataType : "json",
			success : function(data) {
				if (data.result == "success") {
					layer.msg('修改成功!', {
						icon : 6,
						time : 1000
					});
					chang_content(null, 1)
				}
				if (data.result == "failed") {
					layer.msg('修改失败!', {
						icon : 5,
						time : 1000
					});
				}
			},
			error : function(data) {
				layer.msg('请求失败!', {
					icon : 5,
					time : 1000
				});
			}
		});

	}

}

getUpdateSupplierPeopleInfo = function() {
	$("#isAddOrUpdate").val("update");

	var supplierPeopleId = getSelectedNo("update");
	if (supplierPeopleId == "" || supplierPeopleId == null) {
		$("#update").removeAttr("data-target");
		return;
	}
	$("#update").attr("data-target", "#supplier_add");
	$.ajax({
		type : "post",
		url : "/Medical_Instrument/supplierPeople/getUpdateSupplierPeopleInfo",
		data : {
			"supplierPeopleId" : supplierPeopleId
		},
		dataType : "json",
		success : function(data) {
			if (data.result == "success") {
				$("#supplierPeopleInfo").populateForm(
						formatDate(data.supplierPeople));
				$("#business_scope").val(data.instrumentInfo);
				var e = data.enterprise;
				var p_info = "供应商名称：" + e.enterpriseName + ",地址："
						+ e.registerAddress + ";联系人：" + e.linkman + ";联系电话："
						+ e.linkphone;
				$("#p_info").text(p_info);
			} else {
				layer.msg('请求失败!', {
					icon : 5,
					time : 1000
				});
			}
			;
		},
		error : function(data) {
			layer.msg('请求失败!', {
				icon : 5,
				time : 1000
			});
		}
	});
}

// 设置企业经营范围
getBusinessScope = function() {
	if ($("#selectSupplierNo").val() == "") {
		layer.msg('请先选择供应商!', {
			icon : 5,
			time : 1000
		});
		$("#set").removeAttr("data-target");
		return;
	}
	$("#set").attr("data-target", "#setting");

	// 分为新增和修改两种情况
	var str = $("#isAddOrUpdate").val();
	var id;
	if (str == "sign"||str=="select")
		id = getSelectedNoByForm("#verifiers_sign>tbody");
	else
		id = getSelectedNo("update");

	// 当前企业的经营范围
	var enterprise_data;
	// 新增
	$
			.ajax({
				type : "post",
				url : "/Medical_Instrument/enterpriseInstrument/getInstrumentBySupplierNo",
				data : {
					supplier_enterprise_no : $("#selectSupplierNo").val()
				},
				dataType : "json",
				success : function(data) {
					enterprise_data = data;
					if (data != null && str == "add") {
						$("#unselectedCategory").html("");
						var content = "<tr id='theader'><td><input type='checkbox' /></td>"
								+ "<td>分类编号</td>"
								+ "<td>代码编号</td>"
								+ "<td>分类代码</td>" + "<td>管理类别</td></tr>";

						for ( var i = 0; i < data.length; i++) {
							content += "<tr><td><input type='checkbox'/></td>"
									+ "<td>" + data[i].classifyNo + "</td>"
									+ "<td>" + data[i].codeNo + "</td>"
									+ "<td>" + data[i].classifyName + "</td>"
									+ "<td>" + data[i].manageType
									+ "</td></tr>";
						}

						$("#unselectedCategory").html(content);
						$("#record_count").html("合计共 " + data.length + " 条记录");
					} else if (str == "update" || str == "sign"||str=="select") {
						// 修改
						$
								.ajax({
									type : "post",
									url : "/Medical_Instrument/supplierPeopleInstrument/getInstrumentBySupplierPeopleId",
									data : {
										supplierPeopleId : id
									},
									dataType : "json",
									success : function(people_data) {
										$("#selectedCategory").html("");
										var header = "<tr id='theader'><td><input type='checkbox' /></td>"
												+ "<td>分类编号</td>"
												+ "<td>代码编号</td>"
												+ "<td>分类代码</td>"
												+ "<td>管理类别</td></tr>";
										var unselect = header;
										var select = header;
										var stop = false;
										for ( var i = 0; i < people_data.length; i++) {
											if (people_data[i].classifyNo == ""
													|| people_data[i].classifyNo == null) {
												continue;
											}
											select += "<tr><td><input type='checkbox'/></td>"
													+ "<td>"
													+ people_data[i].classifyNo
													+ "</td>"
													+ "<td>"
													+ people_data[i].codeNo
													+ "</td>"
													+ "<td>"
													+ people_data[i].classifyName
													+ "</td>"
													+ "<td>"
													+ people_data[i].manageType
													+ "</td></tr>";
										}
										for ( var k = 0; k < enterprise_data.length; k++) {
											var flag = false;
											for ( var j = 0; j < people_data.length; j++) {
												if (enterprise_data[k].classifyNo == people_data[j].classifyNo) {
													flag = true;
													break;
												}
											}
											if (flag == false) {
												unselect += "<tr><td><input type='checkbox'/></td>"
														+ "<td>"
														+ enterprise_data[k].classifyNo
														+ "</td>"
														+ "<td>"
														+ enterprise_data[k].codeNo
														+ "</td>"
														+ "<td>"
														+ enterprise_data[k].classifyName
														+ "</td>"
														+ "<td>"
														+ enterprise_data[k].manageType
														+ "</td></tr>";

											}

										}

										$("#unselectedCategory").html(unselect);
										$("#selectedCategory").html(select);
										$("#record_count")
												.html(
														"合计共 "
																+ (data.length - people_data.length)
																+ " 条记录");
										$("#record_count_").html(
												"合计共 " + people_data.length
														+ " 条记录");
									}
								});
					}
				}
			});

}

// 删除
delSupplierPeople = function() {
	var obj = getSelectedNo("delete");
	if (obj == null)
		return;
	layer
			.confirm(
					'确认要删除吗？',
					function(index) {
						var supplierPeopleNo = $(obj).children("td").eq(0)
								.text();
						$
								.ajax({
									type : "post",
									url : "/Medical_Instrument/supplierPeople/deleteSupplierPeopleByNo",
									data : {
										"supplierPeopleNo" : supplierPeopleNo
									},
									dataType : "json",
									success : function(data) {
										if (data.result == "success") {
											// 后台删除成功后
											var table = $('#supplier_per')
													.DataTable();
											table.row('.selected').remove()
													.draw(false);

											layer.msg('已删除!', {
												icon : 1,
												time : 1000
											});

										} else {
											layer.msg('删除失败!', {
												icon : 5,
												time : 1000
											});
										}

									},
									error : function(data) {
										layer.msg('请求失败!', {
											icon : 5,
											time : 1000
										});
									}
								});

					});

}

auditor_sign = function() {
	var supplierPeopleId = getSelectedNoByForm("#verifiers_sign>tbody");
	if (supplierPeopleId == null) {
		$("#auditorSign").removeAttr("data-target");
		return;
	}
	$("#confirm_").css("display","none");
	$("#isAddOrUpdate").val("sign");
	$("#verifiersSignButton").css("display", "inline-block");
	$("#confirm").css("display", "none");
	$("#auditorSign").attr("data-target", "#supplier_add");
	$.ajax({
		type : "POST",
		url : "/Medical_Instrument/supplierPeople/getUpdateSupplierPeopleInfo",
		data : {
			"supplierPeopleId" : supplierPeopleId
		},
		dataType : "json",
		success : function(data) {
			if (data.result == "success") {
				$("#supplierPeopleInfo").populateForm(
						formatDate(data.supplierPeople));
				$("#business_scope").val(data.instrumentInfo);
				var e = data.enterprise;
				var p_info = "供应商名称：" + e.enterpriseName + ",地址："
						+ e.registerAddress + ";联系人：" + e.linkman + ";联系电话："
						+ e.linkphone;
				$("#provider_a").removeAttr("data-target");
				$("#provider_a").removeAttr("onclick");
				$("#accreditArea_a").removeAttr("data-target");
				$("#set_button input").attr("disabled", "disabled");
				$("#p_info").text(p_info);
				disableForm("supplierPeopleInfo", true);
			} else {
				layer.msg('请求失败!', {
					icon : 5,
					time : 1000
				});
			}
			;
		},
		error : function(data) {
			layer.msg('请求失败!', {
				icon : 5,
				time : 1000
			});
		}

	});

}

// 在签字弹出框加上当前操作员名字
sign = function() {
	$.ajax({
		type : "POST",
		url : "/Medical_Instrument/operator/getLoginOperator",
		dataType : "json",
		success : function(data) {
			var operator = eval(data);
			$("#operatorName").val(operator.operatorName);
		},
		error : function(data) {
			// 请求失败
			layer.msg('请求失败!', {
				icon : 5,
				time : 1000
			});
		}
	});
}

// 签字保存
function auditorSignSave() {
	var name = $("#operatorName").val();
	var password = $("#operatorPassword").val();
	var supplierPeopleId = getSelectedNoByForm("#verifiers_sign>tbody");
	if (supplierPeopleId == "" || supplierPeopleId == null) {
		layer.msg('发生错误!请选择供方人员后再进行操作!', {
			icon : 5,
			time : 1000
		});
		return;
	}

	if (password == "") {
		layer.msg('请填写密码!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	
	var name = $("#operatorName").val();   
	var pswd = MD5(name + "#"+ password); 
	
	$.ajax({
		type : "POST",
		url : "/Medical_Instrument/supplierPeople/auditSupplierPeople",
		data : {
			"supplierPeopleId" : supplierPeopleId,
			"password" : pswd
		},
		dataType : "json",
		success : function(data) {
			if (data.result == "pwdError") {
				layer.msg('密码错误！', {
					icon : 5,
					time : 1000
				});
				return;
			} else if (data.result == "success") {
				layer.msg('签字成功！', {
					icon : 6,
					time : 1000
				});
				$('.modal').map(function() {
					if (!$(this).is(":hidden")) {
						$(this).modal('hide');
					}
				});
				var table = $('#verifiers_sign').DataTable();
		        table.row('.selected').remove().draw(false);
			} else {
				layer.msg('发生错误!请稍后再试', {
					icon : 5,
					time : 1000
				});

			}

		},
		error : function(data) {
			layer.msg('请求失败!', {
				icon : 5,
				time : 1000
			});
		}
	});
}

//查看
selectSupplierPeople=function(formId){
	var supplierPeopleId = getSelectedNoByForm(formId);
	if(supplierPeopleId==null){
		$(".select").removeAttr("data-target");
		return;
	}
	$("#confirm").css("display","none");
	$("#verifiersSignButton").css("display","none");
	$("#confirm_").css("display","inline-block");
	$("#isAddOrUpdate").val("select");
	$(".select").attr("data-target", "#supplier_add");
	$.ajax({
		type : "post",
		url : "/Medical_Instrument/supplierPeople/getUpdateSupplierPeopleInfo",
		data : {
			"supplierPeopleId" : supplierPeopleId
		},
		dataType : "json",
		success : function(data) {
			if (data.result == "success") {
				$("#supplierPeopleInfo").populateForm(
						formatDate(data.supplierPeople));
				$("#business_scope").val(data.instrumentInfo);
				var e = data.enterprise;
				var p_info = "供应商名称：" + e.enterpriseName + ",地址："
						+ e.registerAddress + ";联系人：" + e.linkman + ";联系电话："
						+ e.linkphone;
				$("#provider_a").removeAttr("data-target");
				$("#provider_a").removeAttr("onclick");
				$("#accreditArea_a").removeAttr("data-target");
				$("#set_button input").attr("disabled", "disabled");
				$("#p_info").text(p_info);
				disableForm("supplierPeopleInfo", true);
			} else {
				layer.msg('请求失败!', {
					icon : 5,
					time : 1000
				});
			}
			;
		},
		error : function(data) {
			layer.msg('请求失败!', {
				icon : 5,
				time : 1000
			});
		}
	});
	
}


//判断当前有没选中行
function getSelectedNo(info) {
	var object = null;
	$("#supplier_per>tbody").find("tr").each(function() {
		if ($(this).hasClass('selected')) {
			if (info != "delete") {
				object = $(this).children("td").eq(0).text();
				if(object=="没有数据")
				     object = null;
			} else {
				object = $(this);
			}
		}
	});
	return object;
}

//根据表格id判断当前有没选中行
function getSelectedNoByForm(formId) {
	var object = null;
	$(formId).find("tr").each(function() {
		if ($(this).hasClass('selected')) {
			if (formId != "delete") {
				object = $(this).children("td").eq(0).text();
				if(object=="没有数据")
				     object = null;
			} else {
				object = $(this);
			}
		}
	});
	return object;
}


// 自动填充表单
$.fn.populateForm = function(data) {
	return this.each(function() {
		var formElem, name;
		if (data == null) {
			this.reset();
			return;
		}
		for ( var i = 0; i < this.length; i++) {
			formElem = this.elements[i];
			// checkbox的name可能是name[]数组形式
			name = (formElem.type == "checkbox") ? formElem.name.replace(
					/(.+)\[\]$/, "$1") : formElem.name;
			if (data[name] == undefined)
				continue;
			switch (formElem.type) {
			case "checkbox":
				if (data[name] == "") {
					formElem.checked = false;
				} else if (data[name] == "1" || data[name] == 1) {
					formElem.checked = true;
				} else {
					// 数查找元素
					if (data[name].indexOf(formElem.value) > -1) {
						formElem.checked = true;
					} else {
						formElem.checked = false;
					}
				}
				break;
			case "radio":
				if (data[name] == "") {
					formElem.checked = false;
				} else if (formElem.value == data[name]) {
					formElem.checked = true;
				}
				break;
			case "button":
				break;
			default:
				formElem.value = data[name];
			}
		}
	});
}

// 处理日期格式
function formatDate(data) {
	var supplierPeople = eval(data);
	var date = new Date();
	if (supplierPeople.accreditStartDate != ""
			&& supplierPeople.accreditStartDate != null) {
		date = new Date(supplierPeople.accreditStartDate);
		supplierPeople.accreditStartDate = date.format("yyyy-MM-dd");
	}
	if (supplierPeople.accreditEndDate != ""
			&& supplierPeople.accreditEndDate != null) {
		date = new Date(supplierPeople.accreditEndDate);
		supplierPeople.accreditEndDate = date.format("yyyy-MM-dd");
	}
	if (supplierPeople.recordsEndDate != ""
			&& supplierPeople.recordsEndDate != null) {
		date = new Date(supplierPeople.recordsEndDate);
		supplierPeople.recordsEndDate = date.format("yyyy-MM-dd");
	}

	return supplierPeople;
}